#include "settingswizard.h"

///
/// Initialiaze the wizard
SettingsWizard::SettingsWizard(QWidget *parent)
    : QWizard(parent)
{
    setPage(Page_CreateDatabase, new CreateDatabasePage);
    setPage(Page_CreateGameLocation, new CreateGameLocationPage);

    setStartId(Page_CreateDatabase);

#ifndef Q_WS_MAC
    setWizardStyle(ModernStyle);
#endif
    setOption(HaveHelpButton, true);
    setPixmap(QWizard::LogoPixmap, QPixmap(":/64_64/logo.png"));

    connect(this, SIGNAL(helpRequested()), this, SLOT(showHelp()));

    setWindowTitle(tr("Settings Wizard"));
}

///
/// Shows the help message of the wizard i.g hints
void SettingsWizard::showHelp()
{
    static QString lastHelpMessage;

    QString message;

    switch (currentId()) {
    case Page_CreateDatabase:
        message = tr("Choose a location where your media database "
                     "will be saved.");
        break;
    default:
        message = tr("This help is likely not to be of any help.");
    }

    if (lastHelpMessage == message)
            message = tr("Sorry, I already gave what help I could. "
                         "Maybe you should try asking a human?");

    QMessageBox::information(this, tr("Settings Wizard Help"), message);
    lastHelpMessage = message;
}

///
/// Triggers the accept the wizard to finish the dialog
void SettingsWizard::accept()
{
    QDialog::accept();
}

///
/// @return     The path to the database
///
/// Returns the absolute path to the database
QString SettingsWizard::databasePath() const
{
    return field("databaseFile").toString();
}

///
/// Initialize the database wizard page
CreateDatabasePage::CreateDatabasePage(QWidget *parent)
    : QWizardPage(parent)
{
    setTitle(tr("Create Database"));
    setSubTitle(tr("This step will create an empty <i>Media Database</i>.\n"
                   "Go to <i>Preferences</i> for advanced settings."));

    topLabel_ = new QLabel(tr("Time to create your own <i>Media Database</i>"));
    topLabel_->setWordWrap(true);

    databaseFileLabel_ = new QLabel(tr("Database File:"));
    databaseFileLineEdit_ = new QLineEdit;
    dataBaseFileButton_ = new QPushButton(tr("Browse..."));

    connect(dataBaseFileButton_, SIGNAL(clicked()), this, SLOT(updateDbFilePath()));

    registerField("databaseFile*", databaseFileLineEdit_);

    /*QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(databaseFileLabel_);
    hLayout->addWidget(databaseFileLineEdit_);
    hLayout->addWidget(dataBaseFileButton_);*/

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(databaseFileLabel_, 0, 0);
    layout->addWidget(databaseFileLineEdit_, 0, 1);
    layout->addWidget(dataBaseFileButton_, 0, 2);
    setLayout(layout);
}

///
/// A event to update the database path in the configuration ini file
void CreateDatabasePage::updateDbFilePath()
{
    QString filePath = QFileDialog::getOpenFileName(this, tr("Specify Location"), QDir::homePath(), tr("Database File (*.db)"));
    if (!filePath.isEmpty()) {
        databaseFileLineEdit_->setText(filePath);
        QSettings settings("steambackup.ini", QSettings::IniFormat);
        settings.setValue("db/path", filePath);
    }
}

///
/// @return     The id of the next wizard page
///
/// Returns the id of the next wizard page
int CreateDatabasePage::nextId() const
{
    return SettingsWizard::Page_CreateGameLocation;
}

///
/// Initialize the game location wizard page
CreateGameLocationPage::CreateGameLocationPage(QWidget *parent)
    : QWizardPage(parent)
{
    setTitle(tr("Choose Game Location"));
    setSubTitle(tr("Choose the location where <i>Steam</i> installed your games.\n"
                   "In general the location is: <i>steamapps</i>"));

    topLabel_ = new QLabel(tr("Time to create your own <i>Media Database</i>"));
    topLabel_->setWordWrap(true);

    locationLabel_ = new QLabel(tr("Game Folder:"));
    gameLocationLineEdit_ = new QLineEdit;
    gameLocationButton_ = new QPushButton(tr("Browse..."));

    connect(gameLocationButton_, SIGNAL(clicked()), this, SLOT(updateGameLocationPath()));

    registerField("gameLocation*", gameLocationLineEdit_);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(locationLabel_, 0, 0);
    layout->addWidget(gameLocationLineEdit_, 0, 1);
    layout->addWidget(gameLocationButton_, 0, 2);
    setLayout(layout);
}

///
/// @return     The id of the next wizard page
///
/// Returns the id of the next wizard page
int CreateGameLocationPage::nextId() const
{
    return -1;
}

///
/// A event to update the game location path in the configuration ini file
void CreateGameLocationPage::updateGameLocationPath()
{
    QString path = QFileDialog::getExistingDirectory(this, tr("Choose Location"), QDir::homePath());
    if (!path.isEmpty()) {
        gameLocationLineEdit_->setText(path);
        QSettings settings("steambackup.ini", QSettings::IniFormat);
        settings.setValue("steam/path", path);
    }
}
